package com.xcc.common;

import java.lang.reflect.Member;
import java.util.HashMap;
import java.util.Map;

/**
 * @outhor Ren
 * @create 2019-03-20 20:55
 * 描述:
 * 通用返回结果集
 */
public class Result {

    /**
     * code 码
     * 0 表示成功
     */
    private int code;
    /**
     * 结果消息
     */
    private String msg;
    /**
     * 结果数据
     */
    private Object data;

    public Result(int code) {
        this.code = code;
    }

    public Result(int code, String msg, Object data) {
        this.code = code;
        this.msg = msg;
        this.data = data;
    }

    /**
     * @return
     */
    public final static Result success() {
        return getResult(0, "success", null);
    }

    public final static Result success(String message) {
        return getResult(0, message, null);
    }

    public final static Result success(String message, Object object) {
        return getResult(0, message, object);
    }

    public final static Result fial(int code, String message) {
        return getResult(code, message, null);
    }

    public final static Result fial(int code, Object object) {
        return getResult(code, "error", object);
    }

    public final static Result fial(int code, String messsage, Object object) {
        return getResult(code, messsage, object);
    }


    public final static Result getResult(int code, String message, Object object) {
        return new Result(code, message, object);
    }

    /**
     * 将结果集转换为 map 对象
     *
     * @return
     */
    public Map<String, Object> toMap() {
        Map<String, Object> result = new HashMap<>();
        result.put("code", code);
        result.put("msg", msg);
        result.put("data", data);
        return result;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
